package com.whfc.wxmp.api.base.service.impl;

import com.whfc.common.entity.DefaultProperties;
import com.whfc.common.enums.EnableState;
import com.whfc.common.enums.SysUserType;
import com.whfc.common.result.ListData;
import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.dto.AppGroupDTO;
import com.whfc.emp.service.AppEmpService;
import com.whfc.emp.service.AppGroupService;
import com.whfc.emp.dto.AppCorpGroupDTO;
import com.whfc.fuum.dto.AppCorpDTO;
import com.whfc.fuum.dto.SysDeptDTO;
import com.whfc.fuum.dto.SysMachListDTO;
import com.whfc.fuum.dto.SysWorkAreaDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.*;
import com.whfc.wxmp.api.base.service.WxOptionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/1 10:09
 */
@Service
public class WxOptionServiceImpl implements WxOptionService {

    /**
     * 默认的演示项目ID
     */
    public static final int DEFAULT_DEMO_PROJECT_ID = 2;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = SysMachTypeService.class, version = "1.0.0")
    private SysMachTypeService sysMachTypeService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @DubboReference(interfaceClass = AppCorpService.class, version = "1.0.0")
    private AppCorpService appCorpService;

    @DubboReference(interfaceClass = AppGroupService.class, version = "1.0.0")
    private AppGroupService appGroupService;

    @DubboReference(interfaceClass = AppEmpService.class, version = "1.0.0")
    private AppEmpService appEmpService;

    @DubboReference(interfaceClass = SysWorkAreaService.class, version = "1.0.0")
    private SysWorkAreaService sysWorkAreaService;

    @Autowired
    private DefaultProperties defaultProps;

    @Override
    public List<SysDeptDTO> getProjectList(WxUser wxUser) {
        //通过手机号查找关联的后台账号->进而查找后台授权的项目
        List<SysDeptDTO> deptList = this.getProjectListByPhone(wxUser.getPhone());
        List<SysDeptDTO> list = new ArrayList<>(deptList);
        //查询app_dept_user,查找小程序账号加入的项目
        //List<SysDeptDTO> deptList2 = appDeptUserMapper.selectDeptListByWxUserId(wxUser.getId());

        //返回项目列表
        //List<SysDeptDTO> deptList = new ArrayList<>(deptList1.size() + deptList2.size());
        //deptList.addAll(deptList1);
        //deptList.addAll(deptList2);

        //演示项目
        if (list.isEmpty()) {
            Integer demoDeptId = defaultProps.getProjectId();
            demoDeptId = ObjectUtils.isEmpty(demoDeptId) ? DEFAULT_DEMO_PROJECT_ID : demoDeptId;
            SysDeptDTO demoProject = sysDeptService.getDeptDTOById(demoDeptId);
            list.add(demoProject);
        }
        return list.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<SysDeptDTO> getDeptList(WxUser wxUser, Integer deptId) {
        List<SysDeptDTO> deptList = sysDeptService.getDescendantDeptListById(deptId);
        return deptList;
    }

    @Override
    public ListData<AppCorpDTO> corpList(Integer deptId) {
        List<AppCorpDTO> list = appCorpService.list(deptId);
        return new ListData<>(list);
    }

    @Override
    public ListData<AppGroupDTO> groupList(Integer deptId) {
        List<AppGroupDTO> list = appGroupService.groupListByProjectId(deptId);
        return new ListData<>(list);
    }

    @Override
    public ListData<AppEmpDTO> getEmpList(Integer deptId) {
        List<AppEmpDTO> appEmpDTOS = appEmpService.list(deptId, null);
        return new ListData<>(appEmpDTOS);
    }

    @Override
    public ListData<SysMachListDTO> machTypeList(Integer machTypeId) {
        ListData<SysMachListDTO> listData = sysMachTypeService.machLists(machTypeId);
        return listData;
    }

    @Override
    public ListData<SysWorkAreaDTO> getWorkAreaList(Integer deptId) {
        List<SysWorkAreaDTO> list = sysWorkAreaService.list(deptId, null);
        return new ListData<>(list);
    }

    /**
     * 通过手机号查询后台账号->查询授权项目
     *
     * @param phone
     * @return
     */
    private List<SysDeptDTO> getProjectListByPhone(String phone) {
        if (!StringUtils.isEmpty(phone)) {
            SysUser sysUser = sysUserService.getUserByPhone(phone);
            if (sysUser != null) {
                List<SysDeptDTO> deptList = new ArrayList<>();
                if (EnableState.DISABLED.getValue().equals(sysUser.getStatus())) {
                    //账号被禁用
                    return deptList;
                }

                Integer userType = sysUser.getType();
                if (SysUserType.INNER.getValue().equals(userType)) {
                    //内部账号
                    deptList = sysDeptService.getAllProjectList();
                } else if (SysUserType.CUSTOMER.getValue().equals(userType)) {
                    //客户用户
                    deptList = sysDeptService.getProjectListByUserId(sysUser.getId());
                } else if (SysUserType.EXPERIENCE.getValue().equals(userType)) {
                    //体验账号 检查账号是否在有效期内
                    Date expireTime = sysUser.getExpireTime();
                    //当前时间戳
                    long nowTimestamp = System.currentTimeMillis();
                    if (nowTimestamp < expireTime.getTime()) {
                        //账号未过期
                        deptList = sysDeptService.getProjectListByUserId(sysUser.getId());
                    }
                }
                return deptList;
            }
        }
        return Collections.emptyList();
    }


    @Override
    public List<AppCorpGroupDTO> corpGroupList(Integer deptId) {
        List<AppCorpDTO> list = appCorpService.list(deptId);
        List<AppCorpGroupDTO> ret = new ArrayList<>();
        list.stream().forEach(appCorpDTO -> {
            List<AppGroupDTO> appGroupDTOS = appGroupService.groupListByCorpId(appCorpDTO.getCorpId());
            AppCorpGroupDTO appCorpGroupDTO = new AppCorpGroupDTO();
            BeanUtils.copyProperties(appCorpDTO,appCorpGroupDTO);
            appCorpGroupDTO.setGroupList(appGroupDTOS);
            ret.add(appCorpGroupDTO);
        });

        return ret;
    }
}
